package com.ruoyi.web.controller.system;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.system.domain.HousekeepingEnclosure;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TrustEnclosure;
import com.ruoyi.system.service.ITrustEnclosureService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 【交易附件】Controller
 * 
 * @author ruoyi
 * @date 2021-06-17
 */
@RestController
@RequestMapping("/system/trustEnclosure")
public class TrustEnclosureController extends BaseController
{
    @Autowired
    private ITrustEnclosureService trustEnclosureService;

    /**
     * 查询【交易附件】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:enclosure:list')")
    @GetMapping("/list")
    public TableDataInfo list(TrustEnclosure trustEnclosure)
    {
        startPage();
        List<TrustEnclosure> list = trustEnclosureService.selectTrustEnclosureList(trustEnclosure);
        return getDataTable(list);
    }

    /**
     * 导出【交易附件】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:enclosure:export')")
    @Log(title = "【交易附件】", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TrustEnclosure trustEnclosure)
    {
        List<TrustEnclosure> list = trustEnclosureService.selectTrustEnclosureList(trustEnclosure);
        ExcelUtil<TrustEnclosure> util = new ExcelUtil<TrustEnclosure>(TrustEnclosure.class);
        return util.exportExcel(list, "enclosure");
    }

    /**
     * 获取【交易附件】详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:enclosure:query')")
    @GetMapping(value = "/{teId}")
    public AjaxResult getInfo(@PathVariable("teId") Long teId)
    {
        return AjaxResult.success(trustEnclosureService.selectTrustEnclosureById(teId));
    }

    /**
     * 新增【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:enclosure:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add( MultipartFile file,TrustEnclosure trustEnclosure)
    {
        try {
            String profile = RuoYiConfig.getProfile();
            byte[] bytes = file.getBytes();
            //文件夹
            Path path2 = Paths.get(profile+"/htId/" + trustEnclosure.getHtId()+"/"+trustEnclosure.getTeType());
            //具体文件
            Path path = Paths.get(profile+"/htId/" + trustEnclosure.getHtId()+"/"+trustEnclosure.getTeType() + "/" + file.getOriginalFilename());
            File file1 = path.toFile();
            //创建文件夹
            if (!file1.exists()) {
                path2.toFile().mkdirs();
            }
            if (file1.isFile()) {
                return AjaxResult.error("文件上传失败,文件存在");
            } else {
                Files.write(path, bytes);//创建文件
                trustEnclosure.setTeUrl("profile/htId/" + trustEnclosure.getHtId()+"/"+trustEnclosure.getTeType() + "/" + file.getOriginalFilename());
            }
        }catch (Exception e){

        }

        return toAjax(trustEnclosureService.insertTrustEnclosure(trustEnclosure));
    }


    /**
     * 修改【交易附件】
     */
//    @PreAuthorize("@ss.hasPermi('system:enclosure:edit')")
    @Log(title = "【交易附件】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TrustEnclosure trustEnclosure)
    {
        return toAjax(trustEnclosureService.updateTrustEnclosure(trustEnclosure));
    }

    /**
     * 删除【交易附件】
     */
//    @PreAuthorize("@ss.hasPermi('system:enclosure:remove')")
    @Log(title = "【交易附件】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{teIds}")
    public AjaxResult remove(@PathVariable Long[] teIds)
    {
        return toAjax(trustEnclosureService.deleteTrustEnclosureByIds(teIds));
    }
}
